﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Weixin.BLL.ChinaTax;
using Weixin.Entity.ChinaTax;
using Weixin.Utils;

namespace Weixin.Web
{
    public partial class Online : System.Web.UI.Page
    {
        int _Cid;
        protected void Page_Load(object sender, EventArgs e)
        {
            int.TryParse(Request.QueryString["Cid"], out _Cid);
            if (_Cid > 0)
            {
                GetData();
            }
            try
            {
                if (Request.QueryString["act"] == "save")
                {
                    ApplicationsBLL _ApplicationsBLL = ApplicationsBLL.GetInstance();
                    ApplicationsEntity _ApplicationsEntity = new ApplicationsEntity();
                    YearMeetingBLL _YearMeetingBLL = YearMeetingBLL.GetInstance();
                    YearMeetingEntity _YearMeetingEntity = new YearMeetingEntity();
                    if (int.Parse(Request.Form["hidCourseId"]) == 156)
                    {
                        List<YearMeetingEntity> list = _YearMeetingBLL.GetYearMeetingList() as List<YearMeetingEntity>;
                        _YearMeetingEntity.Name = Request.Form["txtName"];
                        _YearMeetingEntity.Mobile = Request.Form["txtMobile"];
                        _YearMeetingEntity.Email = Request.Form["txtEmail"];
                        _YearMeetingEntity.Company = Request.Form["txtCompany"];
                        _YearMeetingEntity.CreateTime = DateTime.Now;
                        _YearMeetingEntity.WeiXin = Request.Form["txtWeixin"];
                        _YearMeetingEntity.MeetingDate = Request.Form["ddlDate"];
                        if(_YearMeetingBLL.Insert(_YearMeetingEntity)>0)
                        {
                            Response.Redirect("Success.aspx");
                        }
                    }
                    else
                    {
                        List<ApplicationsEntity> list = _ApplicationsBLL.GetApplicationsList(int.Parse(Request.Form["hidCourseId"])) as List<ApplicationsEntity>;
                        if (list.FindAll(n => n.Name == Request.Form["txtName"] && n.Mobile == Request.Form["txtMobile"]).Count > 0)
                        {
                            Response.Redirect("Success.aspx");
                        }
                        else
                        {
                            _ApplicationsEntity.CourseId = int.Parse(Request.Form["hidCourseId"]);
                            _ApplicationsEntity.CourseName = Request.Form["txtCourseName"];
                            _ApplicationsEntity.Name = Request.Form["txtName"];
                            _ApplicationsEntity.Mobile = Request.Form["txtMobile"];
                            _ApplicationsEntity.Email = Request.Form["txtEmail"];
                            _ApplicationsEntity.Company = Request.Form["txtCompany"];
                            _ApplicationsEntity.CreateTime = DateTime.Now;
                            _ApplicationsEntity.Referee = Request.Form["txtReferee"];
                            _ApplicationsEntity.Status = 0;
                            if (_ApplicationsBLL.Insert(_ApplicationsEntity) > 0)
                            {
                                Response.Redirect("Success.aspx");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Log4Net.Error(ex.Message + ";" + ex.StackTrace);
            }
        }

        void GetData()
        {
            CourseBLL _CourseBLL = CourseBLL.GetInstance();
            CourseEntity _CourseEntity = _CourseBLL.GetAdminSingle(_Cid);
            if (_CourseEntity == null)
            {
                _CourseEntity = new CourseEntity();
            }
            txtCourseName.Value = _CourseEntity.Title;
            hidCourseId.Value = _Cid.ToString();
        }
    }
}